package 作业;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.InternationalFormatter;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Font;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.AbstractListModel;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

public class showticket extends JFrame {

	private static final long serialVersionUID = 1L;
	private userinformation info;
	private String[] ticks;
	private JPanel contentPane;
	private String type="购买";

	
	public showticket(userinformation info,String[] t) throws SQLException {
		this.info=info;
		this.ticks=t;
		init();
	}

	public showticket(String text,userinformation info) throws SQLException {
		this.info=info;
		this.type=text;
		init();
	}
	
	private void init() throws SQLException {
		setTitle("车票信息");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 737, 587);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		
		JScrollPane js = new JScrollPane();
		
		JButton ok = new JButton(type);
		ok.setFont(new Font("宋体", Font.PLAIN, 21));
		
		JButton no = new JButton("取消");
		no.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		no.setFont(new Font("宋体", Font.PLAIN, 21));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap()
							.addComponent(js, GroupLayout.PREFERRED_SIZE, 703, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(244)
							.addComponent(no, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)
							.addGap(125)
							.addComponent(ok, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(15, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(js, GroupLayout.PREFERRED_SIZE, 382, GroupLayout.PREFERRED_SIZE)
					.addGap(65)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(ok)
						.addComponent(no))
					.addContainerGap(50, Short.MAX_VALUE))
		);
		
		JList<String> info1 = new JList<>(type.equals("购买")?ticks:Getdb.op.getinfo(Getdb.username));
		info1.setFont(new Font("宋体", Font.PLAIN, 15));
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//通过集合添加撤票信息
				if(info1.isSelectionEmpty()||info1.getSelectedIndex()==0) {
					JOptionPane.showMessageDialog(info1, "你还未选择车票无法完成"+type,"错误",JOptionPane.ERROR_MESSAGE);
				}
				else {
					if(type.equals("购买")) {
						if(Integer.valueOf(info1.getSelectedValue().split("\s{2,}")[4])!=0) {
							try {
								if(Getdb.op.buyticket(info1.getSelectedValue().split("\s{2,}")[0],info1.getSelectedValue().split("\s{2,}")[1],Getdb.username,Double.valueOf(info1.getSelectedValue().split("\s{2,}")[2]),info1.getSelectedValue().split("\s{2,}")[3]))
								{
									JOptionPane.showMessageDialog(info1,type+"成功","提示",JOptionPane.WARNING_MESSAGE);
									dispose();
								}
								else{
									JOptionPane.showMessageDialog(info1,type+"手慢了，该票被抢光了","提示",JOptionPane.WARNING_MESSAGE);
								}
							} catch (SQLException ex) {
								throw new RuntimeException(ex);
							}
							}
						else {
							JOptionPane.showMessageDialog(info1,type+"失败，该车次无余票","提示",JOptionPane.ERROR_MESSAGE);
						}
					}
					else {
						try {
							Getdb.op.del(Getdb.username,info1.getSelectedValue().split("\s{3,}")[0]);
//							info.del(info1.getSelectedValue());
						} catch (Exception e1) {
							// TODO 自动生成的 catch 块
							e1.printStackTrace();
						}
						JOptionPane.showMessageDialog(info1,type+"成功","提示",JOptionPane.WARNING_MESSAGE);
						dispose();
					}
				}

			}
		});
		js.setViewportView(info1);
		contentPane.setLayout(gl_contentPane);
	}

}
